﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SgAutoArchiveBrrDal;
using SgAutoArchiveBrrDal.Linq;

namespace SgAutoArchiveSp.Dialog
{
    public partial class Dialog_User : DevExpress.XtraEditors.XtraForm
    {
        public Dialog_User()
        {
            InitializeComponent();

            gridView1.CustomDrawCell += GridView1_CustomDrawCell;

            grid.DataSource = T_USER_Dal.Get_User();
        }

        private void GridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "U_I01" || e.Column.FieldName == "U_I02" || e.Column.FieldName == "U_STATUS")
            {
                e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                if (e.CellValue == null || e.CellValue.ToString() == "0")
                {
                    e.DisplayText = "禁止";
                    e.Appearance.ForeColor = Color.Red;
                }
                else if (e.CellValue.ToString() == "1")
                    e.DisplayText = "允许";
                else
                {
                    e.DisplayText = "未知";
                    e.Appearance.ForeColor = Color.Red;
                }
            }
        }

        private void tsb_AddUser_Click(object sender, EventArgs e)
        {
            Dialog.Dialog_UserInfo dialog = new Dialog_UserInfo(null);
            dialog.ShowDialog();
            if(dialog._bOK)
            {
                grid.DataSource = T_USER_Dal.Get_User();
                grid.RefreshDataSource();
            }
        }

        private void tsb_ModifyUser_Click(object sender, EventArgs e)
        {
            int[] ints = gridView1.GetSelectedRows();
            if (ints.Length == 0)
                MessageBox.Show("请先选择要修改的用户数据行!");
            else
            {
                var en = gridView1.GetRow(ints[0]) as T_USER;
                if(en == null)
                {
                    MessageBox.Show("数据未知类型!");
                    return;
                }

                Dialog.Dialog_UserInfo dialog = new Dialog_UserInfo(en);
                dialog.ShowDialog();
                if(dialog._bOK)
                {
                    grid.DataSource = T_USER_Dal.Get_User();
                    grid.RefreshDataSource();
                }
            }
        }

        private void tsb_UserStatus_Click(object sender, EventArgs e)
        {
            int[] ints = gridView1.GetSelectedRows();
            if (ints.Length == 0)
                MessageBox.Show("请先选择要修改的用户数据行!");
            else
            {
                var en = gridView1.GetRow(ints[0]) as T_USER;
                if (en == null)
                {
                    MessageBox.Show("数据未知类型!");
                    return;
                }

                if (MessageBox.Show("确定要禁止选择的用户登录系统吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    return;

                if(T_USER_Dal.Set_UserNotLogin(en.U_ID) == 1)
                {
                    grid.DataSource = T_USER_Dal.Get_User();
                    grid.RefreshDataSource();
                }
                else
                {
                    MessageBox.Show("操作失败!");
                }
            }
        }

        private void txt_UserRefPwd_Click(object sender, EventArgs e)
        {
            int[] ints = gridView1.GetSelectedRows();
            if (ints.Length == 0)
                MessageBox.Show("请先选择要修改的用户数据行!");
            else
            {
                var en = gridView1.GetRow(ints[0]) as T_USER;
                if (en == null)
                {
                    MessageBox.Show("数据未知类型!");
                    return;
                }

                if (MessageBox.Show("确定要重置选择的用户的密码吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    return;

                if (T_USER_Dal.Ref_UserPassword(en.U_ID) == 1)
                {
                    grid.DataSource = T_USER_Dal.Get_User();
                    grid.RefreshDataSource();
                }
                else
                {
                    MessageBox.Show("操作失败!");
                }
            }
        }
    }
}